package bg.btanev.eightyfour.service.pooling.que;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

import org.apache.commons.lang.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author B.Tanev
 *
 */
public class TargetsUpdateQueImpl implements TargetsUpdateQue {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(TargetsUpdateQueImpl.class);

	private BlockingQueue<QueItem> que = new PriorityBlockingQueue<QueItem>();

	public TargetsUpdateQueImpl() {
	}

	public QueItem get() {
		QueItem item = null;
		while (item == null) {
			try {
				item = que.take();
			} catch (InterruptedException e) {
				logger.warn("get() - exception ignored", e); //$NON-NLS-1$
			}
		}

		logger.info("get() - QueItem item=" + item); //$NON-NLS-1$
		assert (item != null);

		return item;
	}

	public void put(QueItem item) {
		Validate.notNull(item);

		logger.info("put(QueItem) - QueItem item=" + item); //$NON-NLS-1$

		boolean added = false;
		while (!added) {
			try {
				que.put(item);
				added = true;
			} catch (InterruptedException e) {
				logger.warn("put(QueItem) - exception ignored", e); //$NON-NLS-1$
			}
		}
	}
}
